package com.lions.client.controller;

import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.lions.client.model.Model;

public class Speech extends HorizontalPanel {

	public Speech(Controller c, Model m) {
		super();
		control = c;
		model = m;

		getElement().setAttribute("x-webkit-speech", "SpeechCommands");
		setStyleName("voicepanel");

		voice = new TextBox();
		voice.getElement().setAttribute("x-webkit-speech", "SpeechCommands");
		voice.setStyleName("microphone");

		voiceInput = new Label();
		voiceInput.setStyleName("micinput");

		add(voice);
		add(voiceInput);

		voice.addFocusHandler(new FocusHandler() {
			@Override
			public void onFocus(FocusEvent event) {
				if (voice.getText() != null) {
					if (voice.getText().equals("add module")) {
						control.getAddButton()
								.fireEvent(new ButtonClickEvent());
					} else if (voice.getText().equals("edit module")) {
						control.getEditButton().fireEvent(
								new ButtonClickEvent());
					} else if (voice.getText().equals("remove module")) {
						control.getModuleRemoveButton().fireEvent(
								new ButtonClickEvent());
					}
					voiceInput.setText(voice.getText());
					voice.setText("");
				}
			}
		});
		// RootPanel.get().addDomHandler(new KeyPressHandler() {
		// @Override
		// public void onKeyPress(KeyPressEvent event) {
		// if (event.getCharCode() == '`') {
		// voice.setFocus(true);
		//
		// }
		// }
		// }, KeyPressEvent.getType());
	}

	public void addVoiceHandler(Button b, String s) {
		button = b;
		newVoiceCommand = s;
		voice.addFocusHandler(new FocusHandler() {
			@Override
			public void onFocus(FocusEvent event) {
				if (voice.getText() != null) {
					System.out.println(voice.getText());
					if (voice.getText().equals(newVoiceCommand)) {
						button.fireEvent(new ButtonClickEvent());
					}
					voice.setText("");
				}
			}
		});
	}

	private Button button;
	private String newVoiceCommand;
	private Controller control;
	private Model model;
	private TextBox voice;
	private Label voiceInput;
}
